********************************
* IV and OLS Analysis for LEHD *
********************************
use "${build}/iv_fin_lehd_w_comp_weights.dta", clear
replace black = 0 if black_miss == 1
replace fem = 0 if fem_miss == 1
keep if ageatcase >=18 & ageatcase<=55 

*Create datasets we will populate with results
cap frame change default
cap frame drop results 
frame create results strL(label) b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evic_all b_judgeamount se_judgeamount

**************************
*Regression specification*
**************************
global cluster_ols="judgexyear"
global cluster_iv="judgexyear"
global stem = ""
global instrument="loo_vc_ivsamp100"
global fe="districtxyearxquarter"

*Outcomes
local outcomes_main = " earn anyearn_allst  anyout13states "

*Define controls
macro define controls="noprior noprior_miss joint ad_damnum ageatcase ageatcase_sq ageatcase_cub fem black white fem_black fem_white hispanic fem_hisp fem_miss black_miss hispanic_miss nbrhd_pov_rate_5yr nbrhd_med_rent_5yr nbrhd_pov_rate_5yr_miss nbrhd_med_rent_5yr_miss"

*Define lagged dependant variable controls
global earn_lags = "earn_b8to1 earn_b8to1_miss earn_b1 earn_b1_miss earn_b2 earn_b2_miss earn_b3 earn_b3_miss earn_b4 earn_b4_miss "
global anyearn_allst_lags = "anyearn_allst_b8to1 anyearn_allst_b8to1_miss anyearn_allst_b1 anyearn_allst_b1_miss anyearn_allst_b2 anyearn_allst_b2_miss anyearn_allst_b3 anyearn_allst_b3_miss anyearn_allst_b4 anyearn_allst_b4_miss"
global b1to8_lags = "earn_b16to9 earn_b9 earn_b10 earn_b11 earn_b12 earn_b16to9_miss earn_b9_miss earn_b10_miss earn_b11_miss earn_b12_miss      anyearn_allst_b16to9 anyearn_allst_b9 anyearn_allst_b10 anyearn_allst_b11 anyearn_allst_b12 anyearn_allst_b16to9_miss anyearn_allst_b9_miss anyearn_allst_b10_miss anyearn_allst_b11_miss anyearn_allst_b12_miss"

*Define judge stringency along money judgment dimension
gen trim_amnt_loo_vc_ivsamp100_s = trimamountloo_vc_ivsamp100/1000
global amnt_instrument = "trim_amnt_loo_vc_ivsamp100_s"	

***********************
* OLS/ IV REGRESSIONS *
***********************
gen all=1
gen sample_restriction =.
gen robust_samp=.
gen imp_robust_samp = . 

foreach pop in "all" "black" "fem"   {
	foreach popval of numlist 0 1 {
		if "`pop'"~="all" | `popval'~=0	{	
			qui  foreach var of varlist `outcomes_main'  {
				foreach rng in "1_4" "5_8" "9_16" "17_24" "b8to1"  {
				
				nois di " ON outcome: `var', pop: `pop'=`popval', time period: `rng' "
				
				if (inlist("`rng'", "b4to1","b8to5","b8to1") & "`pop'" != "all") continue

				*Set lags to missing if outcome window is a pre-period
				if ("`rng'"=="1_4" | "`rng'"=="5_8" | "`rng'"=="9_16" | "`rng'"=="17_24") local lags="$earn_lags $anyearn_allst_lags"
				if ("`rng'"=="b8to1" | "`rng'"=="b8to5" ) local lags=" $b1to8_lags "	
				
				* handling fact b8to1 is both a control and outcomes_main
				if ("`rng'"=="b8to1" ) {
					replace `var'_`rng'= . if `var'_`rng'_miss == 1
				}
				
				*Restricting sample across regressions so obs are the same across specs
				ivregress 2sls `var'_`rng' i.$fe $controls (evicted=$instrument) if  `pop'==`popval', r cluster($cluster_iv) 
				replace sample_restriction = e(sample)
				
				*Non-evict mean
				sum `var'_`rng'   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_mean=r(mean)
				local y_base_sd= r(sd)
				unique pik   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_n= r(sum)
				
				sum `var'_`rng'   if sample_restriction==1  & `pop'==`popval'
				if "`pop'" == "all" local sd_y_all = r(sd)
				if "`pop'" != "all" local sd_y_all = .
				sum evicted  if sample_restriction==1 & `pop'==`popval'
				if "`pop'" == "all" local sd_evic_all = r(sd)
				if "`pop'" != "all" local sd_evic_all = .
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng'"
				frame post results ("`label'")  (.) (.) (.) (`y_base_mean') (`y_base_sd') (`y_base_n') (`sd_y_all') (`sd_evic_all')  (.) (.)
				 
				*OLS with additional controls
				reg  `var'_`rng' evicted $controls `lags' i.$fe   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_ols)
				local b_evicted= _b[evicted]
				local se_evicted=_se[evicted] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Specification: OLS with additional controls" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
				
				*RF with additional controls	
				reg `var'_`rng' $instrument $controls `lags' i.$fe   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_iv) 
				local b_evicted= _b[$instrument]
				local se_evicted=_se[$instrument] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Specification: RF with additional controls" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
				
				*IV: Baseline with additional controls
				ivregress 2sls `var'_`rng' $controls `lags' i.$fe  (evicted=$instrument)   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_iv) 
				local b_evicted= _b[evicted]
				local se_evicted=_se[evicted] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Specification: IV with additional controls" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)

				* Only run the robustness regs on full group and not for outcomes which are also for robustness.
				if ("`pop'" == "all" & ("`var'" != "anyoutstate" & "`var'" != "anyout13states") ) {
					
					*IV: one instrument, one endogenous variable, control for amount stringency
					ivregress 2sls `var'_`rng' (evicted=$instrument) i.$fe $amnt_instrument $controls `lags' if sample_restriction == 1 & `pop'==`popval' & joint==1  & judgment_amount~=., r cluster($cluster_iv) 
					local b_evicted= _b[evicted]
					local se_evicted=_se[evicted] 
					local n_obs=e(N)
					local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Robustness: IV with additional controls, one instrument, one endogenous variable, control for amount stringency"
					frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
					
				}	

					if ("`rng'"=="b8to1" ) {
						replace `var'_`rng'= -1 if `var'_`rng'_miss == 1
					}
				}
			}
		}
	}
}

frame results: export delimited using "${Inputs}/Cook_lehd_ivols.csv", replace 

import delimited "${Inputs}/Cook_lehd_ivols.csv", clear 

split label, parse(",")
rename (label1 label2 label3 label4) (population outcome time_period specification)
rename (b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evic_all) (evic_coeff evic_se obs mean_outcome_non_evic sd_outcome_non_evicted sd_outcome_full_sample sd_evic_full_sample)
drop *judgement*

export delimited using "${Inputs}/Cook_lehd_ivols.csv", replace 